(() => {
  Promise.allConcurrent = (promises, thenForEach) => {
    let callBackQueue = new Array(promises.length);
    let thenCallbackQueue = new Array(promises.length);

    let queueExecutor = () => {
      for (let i = 0, l = callBackQueue.length; i < l; i++) {
        if (callBackQueue[i] == void (0)) {
          return;
        } else {
          callBackQueue[i]();
          callBackQueue[i] = () => { };
        }
      }
      Promise.all(thenCallbackQueue).then(ds => {
        allPromiseResolver(ds);
      });
    };

    let allPromiseResolver = null;
    for (let i = 0, l = promises.length; i < l; i++) {

      promises[i].then(d => {
        callBackQueue[i] = function () {
          thenCallbackQueue[i] = new Promise((resolve) => {
            let result = thenForEach(d,i);
            resolve(result);
          });
        };

        queueExecutor();
      });
    }

    return {
      complete: callback => {
        return new Promise((resolve) => {
          allPromiseResolver = resolve;
        }).then(ds => {
          callback(ds);
        });
      }
    }
  };
})();